<template>
	<view class="fruit-settle-notice">
		温馨提示：请适量点餐，避免浪费哦~
	</view>
	<view class="fruit-settle-address-container">
		<view class="fruit-settle-address-upper">
			<view class="fruit-settle-address-upper-item" 
				:class="data.currentIndex==0?'fruit-settle-address-upper-item-active':''"
				@tap="setIndex(0)">外卖配送</view>
			<view class="fruit-settle-address-upper-item"
				:class="data.currentIndex==1?'fruit-settle-address-upper-item-active':''"
				@tap="setIndex(1)">到店自取</view>
		</view>
		<view class="fruit-settle-address-content">
			<view class="fruit-settle-address-content-inner" v-if="!data.currentIndex">
				<!-- 选择地址部分 -->
				<view class="choose" @tap="showAddress(0)" v-if="data.address.addressIndex == -1">
					<view class="choose-text">点击选择地址</view>
					<image src="../../static/go.png" mode="widthFix"></image>
				</view>
				<u-popup :show="data.address.addressShow" @close="showAddress(1)">
					<view class="fruit-settle-address-selector" :style="{'height':windowHeight*2/3+'px'}">
						<scroll-view scroll-y="true" :style="{'height':windowHeight*2/3-btnHeight +'px'}">
							<view class="fruit-settle-address-select-item" v-for="(item,index) in data.address.addressList" :key="index" @tap="setAddressIndex(index)">
								<view style="width: 600rpx;">
									<view class="fruit-settle-address-select-item-address">
										{{item.address}}
									</view>
									<view class="fruit-settle-address-select-item-region">
										{{item.area}}
									</view>
									<view class="fruit-settle-address-select-item-other">
										<view class="fruit-settle-address-select-item-name">
											{{item.name}}
										</view>
										<view class="fruit-settle-address-select-item-phone">
											{{item.phone}}
										</view>
									</view>
								</view>
								<image src="../../static/go.png" mode="widthFix"></image>
							</view>
						</scroll-view>
						<button class="fruit-settle-address-selector-btn" @tap="goAddress">新增/修改地址</button>
					</view>
				</u-popup>
				<!-- 展示地址部分 -->
				<view class="fruit-settle-address-show" v-if="data.address.addressIndex!=-1">
					<view class="fruit-settle-address-show-item" @tap="showAddress(0)">
						<view class="fruit-settle-address-show-item-address">
							{{data.address.addressList[data.address.addressIndex].address}}
						</view>
						<view class="fruit-settle-address-show-item-region">
							{{data.address.addressList[data.address.addressIndex].area}}
						</view>
						<view class="fruit-settle-address-show-item-other">
							<view class="fruit-settle-address-show-item-name">
								{{data.address.addressList[data.address.addressIndex].name}}
							</view>
							<view class="fruit-settle-address-show-item-phone">
								{{data.address.addressList[data.address.addressIndex].phone}}
							</view>
						</view>
						<image src="../../static/go.png" mode="widthFix"></image>
					</view>
					
				</view>
			</view>
			<view class="fruit-settle-address-content-inner" v-if="data.currentIndex">
				<view class="fruit-settle-address-reserved">
					<view class="fruit-settle-address-reserved-text" style="font-weight: bold;">上海市闵行区东川路500号</view>
				</view>
				<!-- <view class="choose">
					<view class="choose-label">自取时间</view>
					
				</view> -->
				<view class="choose">
					<view class="choose-label">预留电话</view>
					<input v-model="data.phone" type="text" placeholder="请输入电话"/>
				</view>
			</view>
		</view>
	</view>
	<view class="fruit-settle-product-container">
		<view class="fruit-settle-product-item" v-for="(item,index) in data.settleInfo" :key="index">
			<image :src="item.fruitInfo.pic_url" mode="aspectFill"></image>
			<view class="fruit-settle-product-item-text">
				<view class="fruit-settle-product-item-name">
					{{item.fruitInfo.title}}
				</view>
				<view class="fruit-settle-product-item-count">
					<text style="font-size: 25rpx;">x</text>{{item.count}}
				</view>
			</view>
			<view class="fruit-settle-product-item-price">
				<text style="font-weight: normal; font-size: 25rpx;">￥</text>{{item.sumPrice}}
			</view>
		</view>
		<view class="fruit-settle-product-whole">
			<text style="font-size: 30rpx;">小计 ￥</text><text style="font-size: 45rpx; font-weight: bold;">{{data.price}}</text>
		</view>
	</view>
	<view class="fruit-settle-note-container">
		<view class="fruit-settle-note-item" @tap="showInput(0)">
			<view class="fruit-settle-note-label">备注</view>
			<view class="fruit-settle-note-value">{{data.note.outShow}}</view>
		</view>
		<u-popup :show="data.note.noteShow" mode="bottom" @close="closeInput" closeOnClickOverlay="true">
			<view class="fruit-settle-note-input-container">
				<u--textarea border="surround" height="200rpx" v-model="data.note.noteContent" placeholder="请输入备注" count></u--textarea>
				<button class="fruit-settle-note-btn" @tap="showInput(1)">确认</button>
			</view>
		</u-popup>
		<view class="fruit-settle-note-item" @tap="showChop">
			<view class="fruit-settle-note-label">餐具数量</view>
			<view class="fruit-settle-note-value">{{data.chop.chopArr[0][data.chop.chopIndex]}}</view>
			<u-picker :defaultIndex="[data.chop.chopIndex]" :show="data.chop.chopShow" :columns="data.chop.chopArr" @confirm="changeChop" @cancel="showChop" ></u-picker>
		</view>
	</view>
	<view class="fruit-settle-pay-container">
		<radio-group name="pay-group" @change="changePayMethod" class="fruit-settle-pay-group">
			<label class="fruit-settle-pay-item">
				<view class="fruit-settle-pay-item-label">
					<image src="@/page_fruit/static/fruit/wx-pay.png" mode="aspectFit"></image>
					<view class="fruit-settle-pay-item-text">微信支付</view>
				</view>
				<radio :value="0" :checked="data.payIndex == 0"/>
			</label>
			<label class="fruit-settle-pay-item">
				<view class="fruit-settle-pay-item-label">
					<image src="@/page_fruit/static/fruit/zfb-pay.png" mode="aspectFit"></image>
					<view class="fruit-settle-pay-item-text">支付宝支付</view>
				</view>
				<radio :value="1" :checked="data.payIndex == 1" />
			</label>
		</radio-group>
	</view>
	<view style="background-color: white; height: 2rpx;">
		
	</view>
	
	<view class="fruit-foot-container">
		<view class="fruit-foot-price">
			<text>合计￥</text>{{data.price}}
		</view>
		<button class="fruit-foot-btn" @tap="goSettle">提交订单</button>
	</view>
</template>

<script>
	import {onLoad, onShow} from '@dcloudio/uni-app'
	import {reactive,ref, onMounted} from 'vue'
	import {useStore} from 'vuex'
	import {Decimal} from 'decimal.js'
	import {cal} from '../../utils/cal.js'
	import global from '../../config/global.js'
import store from '../../store/index.js'
	export default {
		name:'fruit-settle',
		setup(){
			let data = reactive({
				settleInfo:[],
				price:0,
				currentIndex:0,
				phone:"",
				payIndex:0,
				note:{
					noteContent:"",
					outShow:"请填写备注",
					noteShow:false 
				},
				chop:{
					chopShow:false,
					chopIndex:1,
					chopArr:[['无需餐具','需要餐具,商家依据餐量提供','1份','2份','3份','4份','5份','5份以上']]
				},
				address:{
					addressList:[],
					addressShow:false,
					addressIndex:-1
				}
			})
			
			let windowHeight = uni.getSystemInfoSync()['windowHeight'];
			let btnHeight = uni.upx2px(120);
			let store = useStore()
			
			onShow(()=>{
				getAddress()
			})
			
			onMounted(()=>{
				getAddress()
			})
			
			const chopsticks = ref(null)
			onLoad((e)=>{
				console.log(e)
				data.settleInfo = JSON.parse(decodeURIComponent(e.settleInfo))
				data.price = new Decimal(0)
				for(let item of data.settleInfo){
					item.sumPrice = cal.cheng(item.count,item.fruitInfo.discount_price)
					data.price = cal.jia(data.price,item.sumPrice)
				}
				console.log(data.settleInfo,data.price,typeof(data.price))
			})
			
			function getAddress(){
				uni.request({
					url:global.BASE_URL+'/user/myaddress',
					method:"GET",
					header:{
						"mytoken":uni.getStorageSync('localtoken')
					},
					success:(res)=>{		
						if(res.data.code){
							data.address.addressList = res.data.data
						}
					},
					fail:(err)=>{
						console.log(err)
					}
				})
			}
			
			function setIndex(v){
				data.currentIndex = v
				if(v==1&&data.address.addressIndex!=-1){
					data.phone = data.address.addressList[data.address.addressIndex].phone
				}
			}
			
			function changePayMethod(v){
				data.payIndex = v
			}
			
			function changeChop(e){
				data.chop.chopIndex = e.indexs['0']
				data.chop.chopShow = !data.chop.chopShow
			}
			
			function showChop(){
				data.chop.chopShow = !data.chop.chopShow
			}
			
			function showInput(v){
				if(v==0){
					data.note.noteContent = (data.note.outShow == '请填写备注'?"":data.note.outShow)
					data.note.noteShow = true
				}else{
					data.note.outShow = (data.note.noteContent == ''?'请填写备注':data.note.noteContent)
					data.note.noteShow = false
				}
				
			}
			
			function closeInput(){
				data.note.noteShow = false
			}
			
			function showAddress(v){
				if(v==0||v==1){
					data.address.addressShow = !data.address.addressShow
				}
				
			}
			
			function setAddressIndex(v){
				data.address.addressIndex = v;
				data.address.addressShow = false
			}
			
			function goAddress(){
				uni.navigateTo({
					url:'/pages/me/my-address'
				})
			}
			
			async function dealData(){
				let arr = []
				for(let settle of data.settleInfo){
					let obj = {
						"id":0,
						"order_id":"0",
						"product_id": settle.fruitInfo.id,
						"product_num": settle.count,
						"unit_price": settle.fruitInfo.discount_price
					}
					arr.push(obj);
				}
				return arr;
			}
			
			async function goSettle(){
				if(data.address.addressIndex==-1){
					uni.showToast({
						"icon":"error",
						"title":"尚未选择地址",
						"duration": 500
					})
					return ;
				}
				if(data.price==0){
					uni.showToast({
						"icon":"error",
						"title":"你没选东西"
					})
					uni.navigateBack()
				}
				const arrList = await dealData();
				uni.request({
					url:global.BASE_URL+'/returnparam',
					method:"GET",
					header:{
						"mytoken":uni.getStorageSync("localtoken")
					},
					data:{
						"fruitOrderMain":{
							"total_price": data.price,
							"order_status": 1,
						    "receiver_name": data.address.addressList[data.address.addressIndex].name,
						    "receiver_phone": data.address.addressList[data.address.addressIndex].phone,
						    "receiver_address": data.address.addressList[data.address.addressIndex].area+" "+data.address.addressList[data.address.addressIndex].address,
						    "user_address_id": data.address.addressList[data.address.addressIndex].id,
							"mention": data.note.noteContent,
							"chopsticks":  data.chop.chopArr[0][data.chop.chopIndex]
						},
						"fruitOrderChild": arrList,
						"price": data.price.toString(),
						"title":"朝夕果园——果切购买",
						"type":"1"
					},
					success: (res) => {
						console.log('success',res)
						uni.requestPayment({
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.package,
							signType: res.data.signType,
							paySign: res.data.paySign,
							appId: res.data.appid,
							success (res3) { 
								console.log(res3);
								if(res3.errMsg == "requestPayment:ok"){
									uni.navigateTo({
										url:"/page_fruit/fruit/fruit-settle-order?orderId="+res.data.order_id+"&type=2"
									})
								}
							},
							fail (res3) { 
								console.log(res3);
								if(res3.errMsg == "requestPayment:fail cancel"){
									uni.navigateTo({
										url:"/page_fruit/fruit/fruit-settle-order?orderId="+res.data.order_id+"&type=1"
									})
								}
							},
							complete (res3) { 
								let msg = {
									"msgType": "fruit_order",
									"msgContent": {
										"orderId": res.data.order_id,
										"orderStatus": 1
									}
								}
								store.dispatch('WEBSOCKET_SEND',JSON.stringify(msg))
							}
						})
					},
					fail:(err)=> {
						console.log('fail',err)
					}
				})
			}
			
			return {
				windowHeight,
				btnHeight,
				data,
				
				setIndex,
				changePayMethod,
				changeChop,
				showChop,
				showInput,
				closeInput,
				showAddress,
				setAddressIndex,
				goAddress,
				goSettle
			}
		}
	}
</script>

<style lang="scss" scoped>
	.fruit-settle-notice{
		width: 640rpx;
		padding: 0rpx 20rpx;
		margin: 15rpx 35rpx;
		height: 60rpx;
		font-size: 24rpx;
		line-height: 60rpx;
		background-color: #f2e6ce;
		color: #fca106;
		border-radius: 20rpx;
	}
	
	.fruit-settle-address-container{
		width: 680rpx;
		margin: 10rpx 35rpx;
		border-radius: 20rpx;
		border: 2rpx solid #515151;
		
		& .fruit-settle-address-upper{
			display: flex;
			
			& .fruit-settle-address-upper-item{
				height: 60rpx;
				line-height: 60rpx;
				font-height: 35rpx;
				width: 340rpx;
				text-align: center;
				background-color: #efefef;
				color: 	#888888;
			}
			
			& .fruit-settle-address-upper-item-active{
				background-color: white;
				color: black;
			}
		}
		
		& .fruit-settle-address-content{
			padding: 10rpx 15rpx;
			width: 650rpx;
			
			& .fruit-settle-address-selector{
				width: 660rpx;
				margin: 15rpx 45rpx;
				
				& .fruit-settle-address-selector-btn{
					width: 450rpx;
					height: 70rpx;
					line-height: 70rpx;
					margin-top: 15rpx;
					color: white;
					background-color: #9BCDD2;
				}
				
				& .fruit-settle-address-select-item{
					position: relative;
					width: 640rpx;
					padding: 7rpx 10rpx;
					margin: 10rpx 0;
					border-bottom: 2rpx solid #aaaaaa;
					display: flex;
					align-items: center;
					
					& image{
						width: 40rpx;
						position: absolute;
						right: 20rpx;
					}
					
					& .fruit-settle-address-select-item-address{
						font-size: 35rpx;
						font-weight: bold;
					}
					
					& .fruit-settle-address-select-item-region{
						margin-top: 5rpx;
						font-size: 28rpx;
						color:#888888;
					}
					
					& .fruit-settle-address-select-item-other{
						margin-top: 5rpx;
						display: flex;
						
						& .fruit-settle-address-select-item-name{
							font-size: 28rpx;
							color: #888888;
						}
						
						& .fruit-settle-address-select-item-phone{
							font-size: 28rpx;
							color: #888888;
							margin-left: 20rpx;
						}
					}
				}
			}
			
			& .fruit-settle-address-content-inner{
				
				& .fruit-settle-address-reserved{
					margin: 5rpx 0rpx;
					
					& .fruit-settle-address-reserved-text{
						line-height: 50rpx;
						font-size: 35rpx;
					}
				}
				
				& .fruit-settle-address-show{
					margin: 10rpx 0rpx;
					position: relative;
					
					& .fruit-settle-address-show-item{
						position: relative;
						margin: 10rpx 0;
						// align-items: center;
						
						& .fruit-settle-address-show-item-address{
							width: 550rpx;
							font-size: 35rpx;
							font-weight: bold;
						}
						
						& .fruit-settle-address-show-item-region{
							margin-top: 5rpx;
							font-size: 28rpx;
							color:#888888;
						}
						
						& image{
							width: 40rpx;
							position: absolute;
							right: 20rpx;
							top: 10rpx;
						}
						
						& .fruit-settle-address-show-item-other{
							margin-top: 5rpx;
							display: flex;
							
							& .fruit-settle-address-show-item-name{
								font-size: 28rpx;
								color: #888888;
							}
							
							& .fruit-settle-address-show-item-phone{
								font-size: 28rpx;
								color: #888888;
								margin-left: 20rpx;
							}
						}
					}
				}
				
				& .choose{
					margin: 10rpx 0rpx;
					display: flex;
					height: 70rpx;
					position: relative;
					align-items: center;
					
					& .choose-text{
						font-size: 32rpx;
						height: 70rpx;
						line-height: 70rpx;
						margin-left: 10rpx;
					}
					
					& image{
						position: absolute;
						width: 30rpx;
						right: 20rpx;
					}
					
					& input{
						width: 450rpx;
						padding: 5rpx 10rpx;
						border-bottom: 2rpx solid #888888;
						// background-color: aqua;
					}
					
					& .choose-label{
						width:150rpx;
						text-align: center;
						font-weight: bold;
					}
				}
			}
		}
	}
	
	.fruit-settle-product-container{
		width: 680rpx;
		margin: 10rpx 35rpx;
		border-radius: 20rpx;
		border: 2rpx solid #515151;
		
		& .fruit-settle-product-item{
			position: relative;
			display: flex;
			
			& image{
				margin: 10rpx 10rpx;
				width: 150rpx;
				height: 150rpx;
				border-radius: 180rpx;
			}
			
			& .fruit-settle-product-item-text{
				margin-left: 10rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-around;
				
				
				& .fruit-settle-product-item-name{
					width: 380rpx;
					overflow: hidden;
					white-space: nowrap; 
					text-overflow: ellipsis;
					font-size: 32rpx;
					font-weight: bold;
				}
				
				& .fruit-settle-product-item-count{
					color: #ababab;
					padding: 4rpx 8rpx;
					border: 2rpx solid #ababab;
					height: 30rpx;
					line-height: 30rpx;
					width: fit-content;
					font-size: 28rpx;
					border-radius: 10rpx;
				}
			}
			
			& .fruit-settle-product-item-price{
				position: absolute;
				bottom: 15rpx;
				right: 20rpx;
				font-weight: bold;
				font-size: 32rpx;
			}
			
		}
		
		& .fruit-settle-product-whole{
			width: 630rpx;
			margin: 15rpx 25rpx 17rpx 25rpx;
			padding-top: 15rpx;
			border-top: 2rpx solid #cdcdcd;
			text-align: right;
		}
		
	}
	
	.fruit-settle-note-container{
		width: 680rpx;
		margin: 10rpx 35rpx;
		border-radius: 20rpx;
		border: 2rpx solid #515151;
		
		& .fruit-settle-note-input-container{
			width: 660rpx;
			margin: 15rpx 45rpx;
			height: 400rpx;
			
			& .fruit-settle-note-btn{
				height: 70rpx;
				line-height: 70rpx;
				width: 400rpx;
				margin: 55rpx 130rpx 10rpx 130rpx;
				color: white;
				background-color: #9BCDD2;
			}
		}
		
		& .fruit-settle-note-item{
			display: flex;
			height: 70rpx;
			padding: 10rpx 25rpx;
			align-items: center;
			
			& .fruit-settle-note-label{
				width: 150rpx;
				font-weight: bold;
				font-size: 32rpx;
			}
			
			& .fruit-settle-note-value{
				width: 470rpx;
				text-align: right;
				color: #515151;
			}
		}
	}
	
	
	.fruit-settle-pay-container{
		width: 680rpx;
		margin: 10rpx 35rpx;
		border-radius: 20rpx;
		border: 2rpx solid #515151;
		margin-bottom: 130rpx;
		
		& .fruit-settle-pay-group{
			padding: 15rpx 25rpx;
			
			& .fruit-settle-pay-item{
				display: flex;
				height: 70rpx;
				align-items: center;
				position: relative;
				
				& .fruit-settle-pay-item-label{
					display: flex;
					
					& image{
						width: 40rpx;
						height: 40rpx;
					}
					
					& .fruit-settle-pay-item-text{
						font-size: 26rpx;
						width: 150rpx;
						margin-left: 15rpx;
						font-weight: bold;
					}
				}
				
				& radio{
					position: absolute;
					right: 10rpx;
				}
			}
		}
	}
	
	.fruit-foot-container{
		height: 115rpx;
		width: 750rpx;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		display: flex;
		align-items: center;
		
		
		& .fruit-foot-price{
			text-align: right;
			width: 510rpx;
			font-size: 45rpx;
			font-weight: bold;
			
			& text{
				font-size: 25rpx;
				font-weight: normal;
			}
		}
		
		& .fruit-foot-btn{
			float: right;
			width: 200rpx;
			color: white;
			height: 75rpx;
			line-height: 75rpx;
			border-radius: 200rpx;
			background-color: #9BCDD2;
		}
	}
</style>
